<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Gallery{

        public function __construct(){

            $this->CI = &get_instance();
            $this->CI->load->helper("folder");
            $this->CI->load->model("gallery_model");
        }

        /**
        * 
        * Show error message
        * @param $str error string
        */        
        private function throwError($str){
            echo $str;
            exit();
        }

        /*Insert new gallery in DB*/
        public function add($object_id)
        {
            $path = "gallery_".$object_id;

            // insert into gallery
            $insert_id = $this->CI->gallery_model->insert(array('path'=>$path));
            if(!$insert_id) $this->throwError('Insert gallery to db fail:)');

            return $insert_id;
        } 

        /*Create new gallery folder  */
        public function create_main_direcoty($object_id)
        {

            // create gallery folder for new object
            if(!create_folder("gallery_".$object_id,config_item('gallery_folder')))
                $this->throwError('create gallery folder for new object fail:)');

            return TRUE;

        }

        /*Create all thumbnails directories defined in gallery config */
        public function create_thumbnails_directories($object_id)
        {
            // get thumbnail sizes
            $this->CI->load->model("settings_model");

            // default thumbnails
            $this->CI->settings_model->default_thumbnails();

            // create directories
            $thumbnails = $this->CI->settings_model->list_thumbnails();
            foreach ($thumbnails as $thumbnail) :
                $size = unserialize($thumbnail['user_data']);
                $name = "thumb_".$size['w'].'x'.$size['h'];
                if(!create_folder("gallery_".$object_id."/".$name, config_item('gallery_folder'))) 
                    $this->throwError('create thumbnail folder for gallery fails:)');
                endforeach;


        }

        /*View pictures in gallery*/
        public function view($gallery_id)
        {

            $this->CI->load->model("pictures_model");
            $pictures = $this->CI->pictures_model->find_pictures($gallery_id);
            return $pictures;
        }

        /*View pictures in array*/
        public function view_list($gallery_id)
        {
            $this->CI->load->model("pictures_model");
            $pictures = $this->CI->pictures_model->find_pictures($gallery_id);

            $count = count($pictures);
            $pictures_arr = array();
            for($i=0;$i<$count;$i++){
                $pictures_arr[$i] = $pictures[$i]['filename'];
            }
            
            return $pictures_arr;
        }

        /*Delete gallery and pictures - Delete all folders*/
        public function delete($gallery_id,$folder)
        {
            // Delete gallery and pictures
            $this->CI->gallery_model->delete($gallery_id);
            $path = './'.config_item('gallery_folder').'/'.$folder;
            $this->CI->firephp->log($path);
            delete_folder($path);
        }

    }

?>
